//
//  SYBaseNavController.swift
//  SYWeibo
//
//  Created by Saya on 16/4/8.
//  Copyright © 2016年 黄金果. All rights reserved.
//

import UIKit

class SYBaseNavController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.interactivePopGestureRecognizer?.delegate = self
    }
    //为什么要创建这个base类,是因为通过重写这个方法,设置view的BarButtonItem统一设置样式
    override func pushViewController(viewController: UIViewController, animated: Bool) { 
        //一定要重写这个方法,否则无法正常加载视图,可以在push之前将view的左右barButtonItem设置好
        print(childViewControllers.count)
        if childViewControllers.count > 0 {
            if childViewControllers.count == 1{
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(imgName: "navigationbar_back_withtext", title: "首页", target: self, action: "back")
            }else{
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(imgName: "navigationbar_back_withtext", title: "返回", target: self, action: "back")
            }
            viewController.navigationItem.rightBarButtonItem = UIBarButtonItem(imgName: "navigationbar_friendsearch", title: nil, target: self, action: "friendsearchBtnDidClick")
            viewController.hidesBottomBarWhenPushed = true
            viewController.view.backgroundColor = UIColor.whiteColor()
        }
        super.pushViewController(viewController, animated: animated)
        print(childViewControllers.count)
    }
    
    @objc private func back (){
        print("backDidClick")
        //为什么使用navigationController不能正常执行方法,是因为自己本身就是navigation,所以不能使用navigation
//        navigationController?.popViewControllerAnimated(true)
        self.popViewControllerAnimated(true)
    }
    
    @objc private func friendsearchBtnDidClick (){
        print("friendsearchBtnDidClick")
//        navigationController?.pushViewController(SYTempVC(), animated: true)
        self.pushViewController(SYTempVC(), animated: true)

    }
    
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        print("gestureRecognizerShouldBegin")
        return childViewControllers.count != 1
    }

}